﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CS424_9pts.Control;
using System.Data;
namespace CS424_9pts
{
    public partial class ShoppingCart : System.Web.UI.Page
    {
        CS424_9pts.Control.ShoppingCart cart = new CS424_9pts.Control.ShoppingCart();
        private ShoppingCartViewModel viewModel;

        public ShoppingCartViewModel ViewModel
        {
            get { return viewModel; }
            set { viewModel = value; }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["UserSession"] == null)
            {
                if (!IsPostBack)
                {
                    //Session["ModelId"] = this.Request.QueryString["ModelID"];
                    //Session["BrandId"] = this.Request.QueryString["BrandID"];
                    if (this.Request.QueryString["ModelID"] != null) //Add a new car to cart
                    {
                        this.AddToCart(Int32.Parse(this.Request.QueryString["ModelID"]), Int32.Parse(this.Request.QueryString["BrandID"]));

                    }
                    else
                        GetCart();
                }
                else
                {
                    GetCart();
                    //BindData();
                }
            }
            else
                GetCart(Session["UserSession"].ToString());

            BindData();
            
         }

        private void BindData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Product");
            dt.Columns.Add("ImageURL");
            dt.Columns.Add("Price");
            dt.Columns.Add("Quantity");
            dt.Columns.Add("Total");


            for (int i = 0; i < viewModel.CartItems.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["Product"] = viewModel.CartItems[i].CarModel1.ModelName;
                Image img = new Image();
                img.ImageUrl = viewModel.CartItems[i].CarModel1.ImageName;
                dr["ImageURL"] = img;


                Double? price = viewModel.CartItems[i].CarModel1.Price;
                dr["Price"] = price;
                int? count = viewModel.CartItems[i].Count;
                dr["Quantity"] = count;
                dr["Total"] = price * count;
                dt.Rows.Add(dr);
            }
            this.GridView1.DataSource = dt;
            GridView1.DataBind();
           // GridView1.Columns[1].GetType();
        }

        public void GetCart()
        {
            
            cart.SetCartID(Session.SessionID);
            // Set up our ViewModel
            viewModel = new ShoppingCartViewModel
            {
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };

            // Return the view
        }

        public void GetCart(string userSession)
        {

            cart.SetCartID(userSession);
            // Set up our ViewModel
            viewModel = new ShoppingCartViewModel
            {
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };

            // Return the view
        }

        public CarManagementEntities storeDB = new CarManagementEntities();
        public void AddToCart(int id)
        {

            // Retrieve the album from the database
            var addedCarModel = storeDB.CarModels.Single(
                car => car.ModelID == id);

            // Add it to the shopping cart
            //var cart = ShoppingCart.GetCart(this.HttpContext);

            if(Session["UserSession"] == null)
                cart.SetCartID(Session.SessionID);
            else
                cart.SetCartID(Session["UserSession"].ToString());

            cart.AddToCart(addedCarModel);
            GetCart();
            // Go back to the main store page for more shopping
            
        }


        public void AddToCart(int id, int BrandID)
        {

            // Retrieve the album from the database
            var addedCarModel = storeDB.CarModels.Single(
                car => (car.ModelID == id && car.BrandID == BrandID));

            // Add it to the shopping cart
            //var cart = ShoppingCart.GetCart(this.HttpContext);

            if (Session["UserSession"] == null)
                cart.SetCartID(Session.SessionID);
            else
                cart.SetCartID(Session["UserSession"].ToString());

            cart.AddToCart(addedCarModel);
            GetCart();
            // Go back to the main store page for more shopping

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
          
            foreach (GridViewRow r in GridView1.Rows)
            {
                if ((r.FindControl("CheckBox1") as CheckBox).Checked == true)
                {
                    cart.RemoveFromCart(viewModel.CartItems[r.RowIndex].CarID);
                    this.Page_Load(this, null);
                }

            }
        }

        protected void btnContinueShopping_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }

        protected void btnCheckout_Click(object sender, EventArgs e)
        {
            Label1.Text = "Please contact us with your session: " + Session.SessionID;
            //Response.Redirect("Checkoutshippingaddress.aspx");
            if (Session["isAdmin"] != null)
            {
                Response.Redirect("Checkoutshippingaddress.aspx");
                foreach (Car car in viewModel.CartItems)
                {
                    Session["Cars"] += car.CarID.ToString() + " ";
                }
            }
        }

    }
}